from like.websocket.schemas import PlatformType, DataType


class MessageHandler:
    def __init__(self):
        self.handlers = {
            # 微信处理器
            DataType.WECHAT_GET_PROFILE: self.handle_wechat_get_profile,
            DataType.WECHAT_GET_PRIVATE_MESSAGE_REPLY: self.handle_wechat_private_reply,
            DataType.WECHAT_VIDEO_CHANNEL_PUBLISH: self.handle_wechat_video_publish,
            DataType.WECHAT_MOMENTS_PUBLISH: self.handle_wechat_moments_publish,
            DataType.WECHAT_MOMENTS_INTERACTION: self.handle_wechat_moments_interaction,
            DataType.WECHAT_ADD_TAG: self.handle_wechat_add_tag,
            DataType.WECHAT_ADD_FRIEND: self.handle_wechat_add_friend,
            DataType.WECHAT_INFO_GET: self.handle_wechat_info_get,

            # 小红书处理器
            DataType.XHS_GET_PROFILE: self.handle_xhs_get_profile,
            DataType.XHS_GET_PRIVATE_MESSAGE_REPLY: self.handle_xhs_private_reply,
            DataType.XHS_NOTE_PUBLISH: self.handle_xhs_note_publish,
            DataType.XHS_BEHAVIOR: self.handle_xhs_behavior,
            DataType.XHS_CAPTURE_FLOW: self.handle_xhs_capture_flow,

            # 抖音处理器
            DataType.TIKTOK_GET_PROFILE: self.handle_tiktok_get_profile,
            # DataType.TIKTOK_GET_PRIVATE_MESSAGE_REPLY: self.handle_tiktok_private_reply,
            # DataType.TIKTOK_NOTE_PUBLISH: self.handle_tiktok_note_publish,
            #
            # # 快手处理器
            # DataType.KWAI_GET_PROFILE: self.handle_kwai_get_profile,
            # DataType.KWAI_GET_PRIVATE_MESSAGE_REPLY: self.handle_kwai_private_reply,
            # DataType.KWAI_NOTE_PUBLISH: self.handle_kwai_note_publish,
            #
            # # Boss直聘处理器
            # DataType.BOSS_GET_PROFILE: self.handle_boss_get_profile,
            # DataType.BOSS_PRIVATE_MESSAGE_REPLY: self.handle_boss_private_reply,
            # DataType.BOSS_CONFIG_POSITION: self.handle_boss_config_position,
            # DataType.BOSS_GREET_CANDIDATE: self.handle_boss_greet_candidate,
        }

    def process_message(self, message_dict):
        """处理消息"""
        data_type = message_dict.get("data_type")

        if not data_type:
            return {"error": "Missing data_type field"}

        try:
            data_type_enum = DataType(data_type)
            handler = self.handlers.get(data_type_enum)

            if handler:
                return handler(message_dict)
            else:
                return {"error": f"No handler for data_type: {data_type}"}

        except ValueError:
            return {"error": f"Invalid data_type: {data_type}"}

    # 微信处理方法
    def handle_wechat_get_profile(self, message):
        platform = MessageProcessor.get_platform_from_data_type(message["data_type"])
        return {"status": "success", "platform": platform, "action": "获取微信个人信息"}

    def handle_wechat_private_reply(self, message):
        return {"status": "success", "action": "处理微信私信回复"}

    def handle_wechat_video_publish(self, message):
        return {"status": "success", "action": "微信视频号发布"}

    def handle_wechat_moments_publish(self, message):
        return {"status": "success", "action": "微信朋友圈发布"}

    def handle_wechat_moments_interaction(self, message):
        return {"status": "success", "action": "微信朋友圈互动"}

    def handle_wechat_add_tag(self, message):
        return {"status": "success", "action": "微信添加标签"}

    def handle_wechat_add_friend(self, message):
        return {"status": "success", "action": "微信添加好友"}

    def handle_wechat_info_get(self, message):
        return {"status": "success", "action": "微信信息获取"}

    # 小红书处理方法
    def handle_xhs_get_profile(self, message):
        return {"status": "success", "action": "获取小红书个人信息"}

    def handle_xhs_private_reply(self, message):
        return {"status": "success", "action": "处理小红书私信回复"}

    def handle_xhs_note_publish(self, message):
        return {"status": "success", "action": "小红书笔记发布"}

    def handle_xhs_behavior(self, message):
        return {"status": "success", "action": "小红书互动行为"}

    def handle_xhs_capture_flow(self, message):
        return {"status": "success", "action": "小红书截流"}

    # 其他平台处理方法...
    def handle_tiktok_get_profile(self, message):
        return {"status": "success", "action": "获取抖音个人信息"}

    # ... 其他处理方法


class MessageProcessor:
    @staticmethod
    def get_platform_from_data_type(data_type):
        """从data_type获取平台类型"""
        if 10001 <= data_type <= 10099:
            return PlatformType.WECHAT
        elif 11001 <= data_type <= 11099:
            return PlatformType.XHS
        elif 12001 <= data_type <= 12099:
            return PlatformType.TIKTOK
        elif 13001 <= data_type <= 13099:
            return PlatformType.KWAI
        elif 15001 <= data_type <= 15099:
            return PlatformType.BOSS
        else:
            return None

    @staticmethod
    def is_send_message(data_type):
        """判断是否为发送消息"""
        return 10000 <= data_type <= 19999

    @staticmethod
    def is_receive_message(data_type):
        """判断是否为接收消息"""
        return 20000 <= data_type <= 29999

    @staticmethod
    def get_function_description(data_type):
        """获取功能描述"""
        try:
            data_type_enum = DataType(data_type)
            return data_type_enum.name.replace('_', ' ').title()
        except ValueError:
            return "未知功能"